﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.SerialCommunication.UpperComputer.Core
{
    /// <summary>
    /// 统一处理：数据类型（bool/ushort/float/...）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Result<T>
    {
        /// <summary>
        /// 状态
        /// </summary>
        public bool Status { get; set; } = true;
        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; } = "";
        /// <summary>
        /// 返回的数据
        /// </summary>
        public List<T> Datas { get; set; } = new List<T>();
        /// <summary>
        /// 构造
        /// </summary> 
        public Result() : this(true, "OK") { }
        /// <summary>
        /// 构造默认成功
        /// </summary>
        /// <param name="state"></param>
        /// <param name="msg"></param>
        public Result(bool state, string msg) : this(state, msg, new List<T>()) { }
        /// <summary>
        /// 构造三个参数
        /// </summary>
        /// <param name="state"></param>
        /// <param name="msg"></param>
        /// <param name="datas"></param>
        public Result(bool state, string msg, List<T> datas)
        {
            Status = state; Message = msg; Datas = datas;
        }
        /// <summary>
        /// 静态失败
        /// </summary>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="datas">数据</param>
        /// <returns>错误对象信息</returns>
        public static Result<T> Failed(string errorMessage, List<T> datas = null)
        {
            return new Result<T>(false, errorMessage, datas);
        }
        /// <summary>
        /// 静态成功
        /// </summary>
        /// <param name="datas">数据源</param>
        /// <returns>数据源</returns>
        public static Result<T> Success(List<T> datas = null)
        {
            return new Result<T>(true, "", datas);
        }
    }
}
